//
//  ViewController.m
//  绘图
//
//  Created by 吴志勇 on 2020/3/25.
//  Copyright © 2020 wzy. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

/**  */
@property (nonatomic, weak) UIImageView *panView;

/**  */
@property (nonatomic, assign) CGPoint startP;

/**  */
@property (nonatomic, weak) UIView *coverView;

/*  */
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation ViewController

#pragma mark -------UITableView懒加载
-(UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
        
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
//        [_tableView registerNib:[UINib nibWithNibName:[NSStringFromClass([<#Xib#> Class])] bundle:nil]  forCellReuseIdentifier:<#ID#>];
        [_tableView registerClass:[NSStringFromClass([<#Class#> Class])] forCellReuseIdentifier:<#ID#>];
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            _tableView.rowHeight = UITableViewAutomaticDimension;//自动计算高度
            _tableView.estimatedRowHeight = 40;//预设高度
            _tableView.estimatedSectionFooterHeight = 0;
            _tableView.estimatedSectionHeaderHeight = 0;
        }else{
            self.automaticallyAdjustsScrollViewInsets = NO;
        }

    }
    return _tableView;
}



- (void)viewDidLoad {
    [super viewDidLoad];
//    //0.获取图片
//    UIImage *img = [UIImage imageNamed:@"xx"];
//    //开启跟原始图片一样大小的上下文
//    UIGraphicsBeginImageContextWithOptions(img.size, NO, 0);
//    //绘制一个圆形裁剪区域
//    //1.绘制一个圆形
//    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, img.size.width, img.size.height)];
//    //2.把圆形路径设置成裁剪区域
//    [path addClip];
//    //把图片绘制到上下文当中
//    [img drawAtPoint:CGPointZero];
//    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//    UIGraphicsEndImageContext();
    
    //设置边框宽度
    CGFloat boardW = 10;
    UIImage *image = [UIImage imageNamed:@"xx"];
    //开启y图片的上下文 需要把起点移到边框的原点
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(image.size.width + 2*boardW, image.size.height + 2*boardW), NO, 0);
    //绘制外环圆
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, image.size.width + 2*boardW, image.size.height + 2*boardW)];
    [[UIColor redColor] set];
    [path fill]; // stroke环
    //设置裁剪区域，画笔需要移到图片的原点
    UIBezierPath *clipPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(boardW, boardW, image.size.width, image.size.height)];
    [clipPath addClip];
    CGContextClearRect(<#CGContextRef  _Nullable c#>, <#CGRect rect#>)//擦除区域第一个参数:上下文 第二个:区域
    [image drawAtPoint:CGPointMake(boardW, boardW)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //把控制器的View生成一个图片
    //开启占位图上下文
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0);
    //把控制器的View绘制到上下文当中
    //想要把UIView上面的东西给绘制到上下文当中，必须要得用渲染方式
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [self.view.layer renderInContext:ctx];
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    //把生成的图片写入到桌面
    NSData *imageData  = UIImagePNGRepresentation(image);// UIImageJPEGRepresentation(image, 0.8)  0.8压缩
    [imageData writeToFile:@"xxx" atomically:YES];
   
}

- (void)pictureAScreenshots{
    UIImageView *panView = [[UIImageView alloc] init];
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(AScreenshots:)];
    panView.userInteractionEnabled = YES;
    [panView addGestureRecognizer:pan];
    [self.view addSubview:panView];
    _panView = panView;
}

- (UIView *)coverView{
    if (_coverView == nil) {
        UIView *coverView = [[UIView alloc] init];
        coverView.backgroundColor = [UIColor blackColor];
        coverView.alpha = 0.7;
        _coverView = coverView;
        [self.view addSubview:coverView];
    }
    return _coverView;
}

//
- (void)AScreenshots:(UIPanGestureRecognizer *)pan{
    //判断手势的状态
    CGPoint curP = [pan locationInView:self.panView];
    if (pan.state == UIGestureRecognizerStateBegan) {
        self.startP = curP;
    }else if (pan.state == UIGestureRecognizerStateChanged){
        CGFloat x = self.startP.x;
        CGFloat y = self.startP.y;
        CGFloat w = curP.x - self.startP.x;
        CGFloat h = curP.y - self.startP.y;
        CGRect rect = CGRectMake(x, y, w, h);
        self.coverView.frame = rect;
    }else if (pan.state == UIGestureRecognizerStateEnded){
        UIGraphicsBeginImageContextWithOptions(self.panView.bounds.size, NO, 0);
        UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.coverView.frame];
        [path addClip];
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        [self.panView.layer renderInContext:ctx];
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        [self.coverView removeFromSuperview];
        self.panView.image = newImage;
        
       
        
    }
    
    //每一个UIView内部默认都关联着一个CALayer 我们称这个layer为RootLayer(根层)
    //所有的非Root lLayer，也就是手动创建的CALayer对象，都存在着隐氏动画
    [CATransaction setDisableActions:YES];//Yes 让隐氏动画停止  NO 开启隐氏动画
    
    CALayer *layer = [[CALayer alloc] init];
    [CATransaction begin];
    [CATransaction setDisableActions:NO];
    [CATransaction setAnimationDuration:4.0];
    layer.position = CGPointMake(100, 100);
    layer.bounds = CGRectMake(0, 0, 90, 90);
    layer.backgroundColor = [UIColor redColor].CGColor;
    [CATransaction commit];
    
}
    
    

- (void)uslayer{
    CALayer *layer = [CALayer layer];
    layer.backgroundColor = [UIColor redColor].CGColor;
    layer.frame = CGRectMake(50, 50, 100, 100);
    [self.view.layer addSublayer:layer];
    
    layer.contents = (id)[UIImage imageNamed:@"xx"].CGImage;
    
    //1.创建动画对象(设置layer的属性值)
    CABasicAnimation *anim = [CABasicAnimation animation];
    //2.设置属性
    anim.keyPath = @"position.x";
    anim.toValue = @300;
    
    
    //动画执行次数
    anim.repeatCount = MAXFLOAT;
    //动画执行时长
    anim.duration = 20;
    //动画怎么去怎么回来
    anim.autoreverses = YES;
    
    //动画完成时，会自动删除动画
    anim.removedOnCompletion = NO;
    anim.fillMode = @"forwards";
    
    //添加动画
    [self.view.layer addAnimation:anim forKey:nil];
    
    
    //1.创建动画对象(设置layer的属性值) --->指定动画
    CAKeyframeAnimation *anim1 = [CAKeyframeAnimation animation];
     //2.设置属性
    anim1.keyPath = @"transform.rotation";
    anim.toValue = @[@((-5/180) * M_PI),@((5/180) * M_PI)];
    
    //动画执行次数
    anim.repeatCount = MAXFLOAT;
     //动画怎么去怎么回来
    anim.autoreverses = YES;
    //添加动画
    [self.view.layer addAnimation:anim forKey:nil];
    
    
    //1.创建动画对象(设置layer的属性值) --->指定路径
    CAKeyframeAnimation *anim2 = [CAKeyframeAnimation animation];
     //2.设置属性
    anim2.keyPath = @"position";
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(100, 100)];
    [path addLineToPoint:CGPointMake(300, 200)];
    anim2.path = path.CGPath;
  
    
    //动画执行次数
    anim.repeatCount = MAXFLOAT;
     //动画怎么去怎么回来
    anim.autoreverses = YES;
    //添加动画
    [self.view.layer addAnimation:anim forKey:nil];
    
    
    //转场代码与转场动画必须在同一个方法
    CATransition *anim3 = [CATransition animation];
    anim3.type = @"suckEffect";
    anim3.startProgress = 0.5;//从什么地方开始动画
    anim3.endProgress = 0.6;//动画结束为止
    anim3.duration = 2;
    [self.view.layer addAnimation:anim3 forKey:nil];
    
    
   /**
    1.UIView动画和CAAnimation区别
        1.核心动画只作用在Llayer上；
        2.核心动画都是假象，不会改变真实位置
    
        什么时候使用核心动画
        1.当不需要与用户进行交互，使用核心动画
        2.当要根据路径做动画，使用核心动画
        3.当做转场动画，x使用核心动画
    */
    
    //原始大图片
    UIImage *oriImageimage = [UIImage imageNamed:@"xx"];
    CGFloat x = 0;
    CGFloat y = 0;
    //设置屏幕裁剪比例
    CGFloat clipW = oriImageimage.size.width/12.0*[UIScreen mainScreen].scale;
    CGFloat clipH = oriImageimage.size.height*[UIScreen mainScreen].scale;
    CGImageRef imgRef = CGImageCreateWithImageInRect(oriImageimage.CGImage, CGRectMake(x, y,  clipW, clipH));
    UIImage *newImage = [UIImage imageWithCGImage:imgRef];
    
    
    //渐变层
    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = self.view.bounds;
    //设置渐变的颜色
    gradient.colors = @[(id)[UIColor redColor].CGColor,(id)[UIColor yellowColor].CGColor,(id)[UIColor blueColor].CGColor];
    //设置渐变的方向
    gradient.startPoint = CGPointMake(0, 0);
    gradient.endPoint = CGPointMake(1, 0);
    
    //设置一个渐变到另一个渐变的起始位置
    gradient.locations = @[@0.2,@0.6];
    [self.view.layer addSublayer:gradient];
    
}

@end
